package com.sundaying.gooway.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/*
 * 用户访问URL权限过滤器
 */
public class PermissionFilter implements Filter {

	@Override
	public void destroy() {
		System.out.println("销毁过滤器PermissionFilter...");
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) arg0;
		HttpServletResponse response = (HttpServletResponse) arg1;
		
		HttpSession session = request.getSession();
		String url = request.getRequestURI();
		System.out.println("请求路径：" + url);
		// 判断后台是否登陆，如果没登录则跳转到登录页面
//		if (!url.contains("login.jsp") && session.getAttribute("user") == null) {
//			System.out.println("用户未登陆，跳转到：" + url);
//			response.sendRedirect(request.getContextPath() + "/admin/login.jsp");
//			return;
//		}

		System.out.println("过滤器PermissionFilter验证通过");
		arg2.doFilter(arg0, arg1); // 如果登陆直接向下执行
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("初始化过滤器PermissionFilter...");
	}

}
